// #include<iostream>
// #include<vector>
// #include<opencv2/opencv.hpp>
// #include<opencv2/core/core.hpp>
// #include<opencv2/highgui/highgui.hpp>
// #include<opencv2/imgproc/imgproc.hpp>

// using namespace std;
// using namespace cv;

// // circle(image2, points2, 3, Scalar(255, 0, 120), 30);//画圆，空心的
// // circle(image1, points1, 3, Scalar(0, 255, 120), -1);//画点，其实就是实心圆

// void save_data_toFile(const char * path,uint8_t * mem,uint32_t bytes)
// {
//     FILE * fp = fopen(path,"wb");
//     fwrite(mem,sizeof(uint8_t),bytes,fp);
//     fclose(fp);
// }

// void open_data_toMem(const char * path,uint8_t * mem,uint32_t bytes)
// {
//     FILE * fp = fopen(path,"rb");
//     fread(mem,sizeof(uint8_t),bytes,fp);
//     fclose(fp);
// }

// #if 0
// int main(){
// 	Mat image1 = imread("../data/640x800.jpg", 1);
// 	vector<Point2f> points1;
	
// 	// points1.push_back(Point2f(2, 3));  
	
//     for (int i = 160;i < 480;i+=20)
//     {
//         for (int j = 200;j < 600;j+=20)
//         {
//             points1.push_back(Point2f(i, j));
//         }
//     }
    
//     cout << points1.size() << endl;
// 	for(int i = 0; i < points1.size(); i++){
// 		circle(image1, points1[i], 1, Scalar(0, 255, 0), -1);//画点，其实就是实心圆
// 	}

// 	imshow("PointsinImage", image1);
// 	waitKey(0);
    
// 	return 0;
// }
// #else
// int main(){

// 	uint8_t * data_ptr = (uint8_t *)malloc(640*800);

// 	open_data_toMem("../data/640*800.raw",data_ptr,640*800);

// 	// Mat type, Size(width,height) ---> (cols,rows)
// 	Mat image1 = cv::Mat(cv::Size(640,800),CV_8UC1,data_ptr);

// 	vector<Point2f> points1;
	
// 	// points1.push_back(Point2f(2, 3));  
	
//     for (int i = 160;i < 480;i+=20)
//     {
//         for (int j = 200;j < 600;j+=20)
//         {
//             points1.push_back(Point2f(i, j));
//         }
//     }
    
//     cout << points1.size() << endl;
// 	for(int i = 0; i < points1.size(); i++){
// 		circle(image1, points1[i], 1, Scalar(0, 255, 255), -1);//画点，其实就是实心圆
// 	}

// 	imshow("PointsinImage", image1);
// 	waitKey(0);
// 	free(data_ptr);
// 	return 0;
// }
// #endif


#include <opencv2/opencv.hpp>

int main() {
    // 读取图像
    cv::Mat image = cv::imread("./data/640x800.jpg");

    if (image.empty()) {
        std::cerr << "Error: Could not read the image." << std::endl;
        return -1;
    }

    // 定义直方图的参数
    int histSize = 256; // 直方图中的条柱数量
    float range[] = { 0, 256 }; // 像素值范围
    const float* histRange = { range };

    // 分割RGB通道
    std::vector<cv::Mat> channels;
    cv::split(image, channels);

    // 计算红色通道的直方图
    cv::Mat red_hist;
    cv::calcHist(&channels[2], 1, 0, cv::Mat(), red_hist, 1, &histSize, &histRange);

    // 计算绿色通道的直方图
    cv::Mat green_hist;
    cv::calcHist(&channels[1], 1, 0, cv::Mat(), green_hist, 1, &histSize, &histRange);

    // 计算蓝色通道的直方图
    cv::Mat blue_hist;
    cv::calcHist(&channels[0], 1, 0, cv::Mat(), blue_hist, 1, &histSize, &histRange);

    // 归一化直方图
    cv::normalize(red_hist, red_hist, 0, 255, cv::NORM_MINMAX, -1, cv::Mat());
    cv::normalize(green_hist, green_hist, 0, 255, cv::NORM_MINMAX, -1, cv::Mat());
    cv::normalize(blue_hist, blue_hist, 0, 255, cv::NORM_MINMAX, -1, cv::Mat());

    // 创建一个直方图图像
    int hist_w = 512;
    int hist_h = 400;
    cv::Mat hist_image(hist_h, hist_w, CV_8UC3, cv::Scalar(0, 0, 0));

    // 绘制红色通道直方图
    for (int i = 1; i < histSize; i++) {
        cv::line(hist_image, cv::Point(i - 1, hist_h - cvRound(red_hist.at<float>(i - 1))),
            cv::Point(i, hist_h - cvRound(red_hist.at<float>(i))),
            cv::Scalar(0, 0, 255), 2, 8, 0);
    }

    // 绘制绿色通道直方图
    for (int i = 1; i < histSize; i++) {
        cv::line(hist_image, cv::Point(i - 1, hist_h - cvRound(green_hist.at<float>(i - 1))),
            cv::Point(i, hist_h - cvRound(green_hist.at<float>(i))),
            cv::Scalar(0, 255, 0), 2, 8, 0);
    }

    // 绘制蓝色通道直方图
    for (int i = 1; i < histSize; i++) {
        cv::line(hist_image, cv::Point(i - 1, hist_h - cvRound(blue_hist.at<float>(i - 1))),
            cv::Point(i, hist_h - cvRound(blue_hist.at<float>(i))),
            cv::Scalar(255, 0, 0), 2, 8, 0);
    }

    // 显示原始图像和RGB三色直方图
    cv::imshow("mage", image);
    cv::imshow("RGB Histogram", hist_image);

    cv::waitKey(0);

    return 0;
}


